package cn.sumpu.app.coolsk.web.mvc.front;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.coolsk.biz.ContentBiz;
import cn.sumpu.app.coolsk.biz.DiscussBiz;
import cn.sumpu.app.coolsk.domain.Content;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;

@Controller
public class ListController {
	
	
	private final static Logger logger = LoggerFactory.getLogger(ListController.class);
	@Autowired
	private ContentBiz contentBiz;
	@Autowired
	private DiscussBiz discussBiz;
	
	@RequestMapping(value = "/listView.htm")
	public String listView(String channel,Integer page,HttpServletRequest request,HttpServletResponse response) throws AppBusinessException{
		request.setAttribute("channel", channel);
		try {
			int pageSize = 20;
			PageModel pageModel = contentBiz.getListViewContentByChannel(channel, pageSize, page);
			request.setAttribute("pageModel", pageModel);
			//景区满意度排行
			List<Content> indexScenicList = contentBiz.getContentByChannel("8e19272f-1a05-4e4b-85e0-9660d4770456", 5);
			getDiscount(indexScenicList);
			request.setAttribute("indexScenicList", indexScenicList);
			//酒店满意度排行
			List<Content> indexHotelList = contentBiz.getContentByChannel("b9ce5fab-e3b9-42c9-9d9e-b981a3763fb2", 5);
			getDiscount(indexHotelList);
			request.setAttribute("indexHotelList", indexHotelList);
			//爽客热评
			List<Content> indexCommentList = contentBiz.getDiscussByChannel("c9993029-3df9-4609-a294-e6f44a19e029", 4);
			request.setAttribute("indexCommentList", indexCommentList);
			return "v1/search/searchContent";
		} catch (AppBusinessException e) {
			e.printStackTrace();
			if (logger.isErrorEnabled()) {
				logger.error("Get hotel failure.", e);
			}
			return "v1/common/error";
		}
	}
	//获取点评数
	private void getDiscount(List<Content> jingQuManYiDu)
			throws AppBusinessException {
		for (Content content : jingQuManYiDu) {
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("owner", content.getTblId());
			if(content.getTblName().equals("coolsk_hotel")){
				map.put("ownerType", "hotel");
			}else if(content.getTblName().equals("coolsk_scenic")){
				map.put("ownerType", "att");
			}
			int disCount=discussBiz.getDiscussCountByObj(map);
			content.setDisCount(disCount);
		}
	}
}
